package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.util.PageBean;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import org.apache.zookeeper.OpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("brand")
public class BrandController {
    private Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private BrandService brandService;

    @RequestMapping("index")
    public String index(){
        return "/admin/index";
    }



    @RequestMapping("home")
    public String home(){
        return "/admin/home";
    }

    @RequestMapping("toBrand")
    public String toBrand(){
        return "admin/brand";
    }

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(@RequestParam(defaultValue = "1")Integer number,@RequestParam(defaultValue = "5")Integer pageSize){
        try {
            PageBean<TbBrand> b = brandService.getinfo(number,pageSize);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),b);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }


    @RequestMapping("addinfo")
    @ResponseBody
    public dataResult addinfo(TbBrand tbBrand){
        if (tbBrand == null){
            return new dataResult(ResponseStatus.BAD_REQUEST.getIndex(), ResponseStatus.BAD_REQUEST.getName());
        }
        try {
            Integer i = brandService.addinfo(tbBrand);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("增加失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public dataResult deleteAll(@RequestParam("ids[]")Integer[] ids){
        if (ids == null){
            return new dataResult(ResponseStatus.BAD_REQUEST.getIndex(), ResponseStatus.BAD_REQUEST.getName());
        }
        try {
            Integer i = brandService.deleteAll(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("删除失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public dataResult update(Long id,Model model){
        if (id == null){
            return new dataResult(ResponseStatus.BAD_REQUEST.getIndex(), ResponseStatus.BAD_REQUEST.getName());
        }
        try {
            TbBrand tbBrand = brandService.getUpdate(id);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),tbBrand);
        }catch (Exception e){
            logger.error("删除失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("updateinfo")
    @ResponseBody
    public dataResult updateinfo(TbBrand tbBrand){
        if (tbBrand == null){
            return new dataResult(ResponseStatus.BAD_REQUEST.getIndex(), ResponseStatus.BAD_REQUEST.getName());
        }
        try {
            Integer i = brandService.updateinfo(tbBrand);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("修改失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }
}
